package com.puzzlebazar.client.main.model;

import java.util.HashMap;

import com.puzzlebazar.client.user.UserInfo;

public abstract class PuzzleListInfoWithType extends PuzzleListInfoImpl {

	private static final String LIST_TYPE = "Type";

	protected final ListType listType;	
	
	/**
	 * Extracts a list type parameter
	 * 
	 * @param params The list of parameters for the puzzle list info (will be modified, the list type will be removed)
	 * @return The extracted list time
	 */
	private static ListType extractListType( HashMap<String,String> params ) {
		try {
			return ListType.valueOf( params.remove(LIST_TYPE) );
		}
		catch( Exception exception ) {
			return ListType.ALL_PUZZLES;
		}		
	}

	/**
	 * Creates a puzzle list information structure for a query having a list type
	 * 
	 * @param infoType The type of this puzzle list information structure
	 * @param params A parameter list containing a LIST_TYPE parameter
	 */
	public PuzzleListInfoWithType( String infoType, HashMap<String, String> params) {
		super(infoType);
		listType = extractListType( params );
		appendCustomParameters();
	}

	/**
	 * Creates a puzzle list information structure for a query having a list type
	 * 
	 * @param infoType The type of this puzzle list information structure
	 * @param listType The list type to use
	 */
	public PuzzleListInfoWithType( String infoType, ListType listType ) {
		super(infoType);
		this.listType = listType;		
		appendCustomParameters();
	}

	@Override
	public boolean canUserGetList(UserInfo userInfo) {
		if( listType == ListType.EDITABLE_PUZZLES ) {
			if( userInfo == null )
				return false;
		}
		return true;
	}	

	/**
	 * Append the custom parameters for this class. 
	 * Should only be called by a constructor.
	 */
	private void appendCustomParameters() {
		appendCustomParam( LIST_TYPE, listType.toString() );
	}
	
}
